package com.binarysoft.service.common.snmp;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import common.framework.dsb.client.DefaultServiceFactory;
import common.framework.dsb.client.ServiceFactory;

public class SnmpServiceExample {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// 设置被采集的设备的SNMP信息
		SnmpTarget snmpTarget = new SnmpTarget();
		snmpTarget.setSnmpVersion(SnmpService.VERSION2C);
		snmpTarget.setHostAddress("127.0.0.1");
		snmpTarget.setSnmpPort(161);
		snmpTarget.setReadCommunity("public");

		// 构造被采集的对象的集合
		Collection<SnmpObject> objects = createObjects();

		// SNMP 远程服务的信息
		String serviceName = "DSB/SnmpService";
		String hostAddress = "127.0.0.1";
		int port = 10000;

		// 创建服务工厂类
		ServiceFactory serviceFatory = new DefaultServiceFactory();
		final SnmpService snmpService = (SnmpService) serviceFatory.lookup(serviceName, hostAddress, port);

		Thread th = new Thread() {
			public void run() {
				for (int i = 0; i < 50; i++) {
					int size = snmpService.getPoolSize();
					System.out.println("pool size = " + size);
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
					}
				}
			}
		};
		th.start();

		// 调用远程服务采集数据
		SnmpResult snmpResult = snmpService.snmpGet(objects, snmpTarget);
		// 迭代输出结果集
		Iterator<SnmpValue> iter = snmpResult.iterator();
		while (iter.hasNext()) {
			SnmpValue snmpValue = iter.next();
			System.out.println(snmpValue.getObjectName() + "." + snmpValue.getIndex() + "=" + snmpValue.getValue());
		}
	}

	public static Collection<SnmpObject> createObjects() {
		Set<SnmpObject> objects = new LinkedHashSet<SnmpObject>();

		SnmpObject o1 = new SnmpObject();
		o1.setName("sysDescr");
		o1.setOid("1.3.6.1.2.1.1.1.0");
		o1.setObjectType(SnmpService.SINGLE_INSTANCE_OBJECT);
		objects.add(o1);

		SnmpObject o2 = new SnmpObject();
		o2.setName("sysObjectID");
		o2.setOid("1.3.6.1.2.1.1.2.0");
		o2.setObjectType(SnmpService.SINGLE_INSTANCE_OBJECT);
		objects.add(o2);

		SnmpObject o3 = new SnmpObject();
		o3.setName("sysUpTime");
		o3.setOid("1.3.6.1.2.1.1.3.0");
		o3.setObjectType(SnmpService.SINGLE_INSTANCE_OBJECT);
		objects.add(o3);

		SnmpObject o4 = new SnmpObject();
		o4.setName("sysContact");
		o4.setOid("1.3.6.1.2.1.1.4.0");
		o4.setObjectType(SnmpService.SINGLE_INSTANCE_OBJECT);
		objects.add(o4);

		SnmpObject o5 = new SnmpObject();
		o5.setName("sysName");
		o5.setOid("1.3.6.1.2.1.1.5.0");
		o5.setObjectType(SnmpService.SINGLE_INSTANCE_OBJECT);
		objects.add(o5);

		SnmpObject o6 = new SnmpObject();
		o6.setName("sysLocation");
		o6.setOid("1.3.6.1.2.1.1.6.0");
		o6.setObjectType(SnmpService.SINGLE_INSTANCE_OBJECT);
		objects.add(o6);

		SnmpObject o7 = new SnmpObject();
		o7.setName("sysServices");
		o7.setOid("1.3.6.1.2.1.1.7.0");
		o7.setObjectType(SnmpService.SINGLE_INSTANCE_OBJECT);
		objects.add(o7);

		SnmpObject o8 = new SnmpObject();
		o8.setName("ifIndex");
		o8.setOid("1.3.6.1.2.1.2.2.1.1");
		o8.setObjectType(SnmpService.MULTI_INSTANCE_OBJECT_WITHOUT_INDEX);
		// objects.add(o8);

		SnmpObject o9 = new SnmpObject();
		o9.setName("udpLocalPort");
		o9.setOid("1.3.6.1.2.1.7.5.1.2");
		o9.setObjectType(SnmpService.MULTI_INSTANCE_OBJECT_WITHOUT_INDEX);
		objects.add(o9);

		SnmpObject o10 = new SnmpObject();
		o10.setName("ipAdEntAddr");
		o10.setOid("1.3.6.1.2.1.4.20.1.1");
		o10.setObjectType(SnmpService.MULTI_INSTANCE_OBJECT_WITHOUT_INDEX);
		objects.add(o10);

		SnmpObject o11 = new SnmpObject();
		o11.setName("ifInOctets");
		o11.setOid("1.3.6.1.2.1.2.2.1.10");
		o11.setObjectType(SnmpService.MULTI_INSTANCE_OBJECT_WITH_INDEX);
		o11.setIndexObject(o8);
		objects.add(o11);

		SnmpObject o12 = new SnmpObject();
		o12.setName("ifOutOctets");
		o12.setOid("1.3.6.1.2.1.2.2.1.16");
		o12.setObjectType(SnmpService.MULTI_INSTANCE_OBJECT_WITH_INDEX);
		o12.setIndexObject(o8);
		objects.add(o12);

		SnmpObject o13 = new SnmpObject();
		o13.setName("ifDescr");
		o13.setOid("1.3.6.1.2.1.2.2.1.2");
		o13.setObjectType(SnmpService.MULTI_INSTANCE_OBJECT_WITH_INDEX);
		o13.setIndexObject(o8);
		objects.add(o13);
		return objects;
	}
}
